package com.ykdl.member.kid.adapters;

import java.util.ArrayList;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.TopCatalogDoctorAdapter.ViewHolder1;
import com.ykdl.member.kid.adapters.TopCatalogDoctorAdapter.ViewHolder2;
import com.ykdl.member.kid.gears.TopicsSheetListActivity;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogItemBean;
import com.ykdl.member.kid.models.CatalogTopicItem;
import com.ykdl.member.kid.models.TopicBean;
import com.ykdl.member.kid.util.DateUtil;
import com.ykdl.member.kid.util.MobileUtils;
import com.ykdl.member.kid.widget.AsyncImageView;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class DoctorSheetListAdapter extends BaseAdapter implements OnClickListener {
	private TopicsSheetListActivity activity;
	private ArrayList<CatalogItemBean> catalog_item_list = null;
	private BitmapUtils  bitmapUtil;
	public DoctorSheetListAdapter(Activity activity, BitmapUtils bitmapUtil) {
		this.activity = (TopicsSheetListActivity) activity;
		this.bitmapUtil = bitmapUtil;
	}

	public void setData(ArrayList<CatalogItemBean> datas) {
		if (catalog_item_list == null) {
			catalog_item_list = new ArrayList<CatalogItemBean>();
		}
		catalog_item_list.addAll(datas);
	}

	@Override
	public int getCount() {
		return catalog_item_list != null && catalog_item_list.size() > 0?catalog_item_list.size():0;
	}

	@Override
	public Object getItem(int position) {
		return catalog_item_list != null && catalog_item_list.size() > 0?catalog_item_list.get(position):null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public int getItemViewType(int position) {
		
		if(((CatalogTopicItem)catalog_item_list.get(position)).getAnswer() == null || 
				"".equals(((CatalogTopicItem)catalog_item_list.get(position)).getAnswer())||
				"null".equals(((CatalogTopicItem)catalog_item_list.get(position)).getAnswer())){
			return 1;
		}else{
			return 2;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder1 holder1 = null;
		ViewHolder2 holder2 = null;
		int type = getItemViewType(position);
		if (convertView == null) {
			if (type == 1) {
				convertView = LayoutInflater.from(activity).inflate(R.layout.iask2_doc_item1, null);
				holder1 = new ViewHolder1();
				setView1(holder1, convertView);
				convertView.setTag(holder1);
			} else {
				convertView = LayoutInflater.from(activity).inflate(R.layout.iask2_doc_item2, null);
				holder2 = new ViewHolder2();
				setView2(holder2, convertView);
				convertView.setTag(holder2);
			}
		} else {
			if (type == 1) {
				holder1 = (ViewHolder1) convertView.getTag();
			} else {
				holder2 = (ViewHolder2) convertView.getTag();
			}
		}
		CatalogTopicItem  item = (CatalogTopicItem) catalog_item_list.get(position);
		if (type == 1) {// 只有问题
			holder1.item_content.setText(item.getItem().getTitle());
			//设置宝宝年龄
			if (item.getItem().getExtra_data()!=null) {
				 String strAge = DateUtil.getDayTip(item.getItem().getExtra_data().getBaby_days());
				holder1.item_age.setText(strAge);//宝宝年龄
			}
			//设置发布时间
			holder1.item_time.setText(DateUtil.gettopicDate(item.getItem().getPost_time()));
		} else {//一问一答
			holder2.item_question_content.setText(item.getItem().getTitle());
			holder2.item_answer_content.setText(item.getItem().getAnswer().getMessage());
			if (item.getItem().getAnswer().getPost_actor().getAvatar_file_meta()!=null) {
				String url = item.getItem().getAnswer().getPost_actor().getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
				bitmapUtil.configDefaultLoadingImage(R.drawable.doctor_defuat_big_icon);
				bitmapUtil.configDefaultLoadFailedImage(R.drawable.doctor_defuat_big_icon);
				bitmapUtil.display(holder2.head, url);//医生头像
			}
			holder2.name.setText(item.getItem().getAnswer().getPost_actor().getDisplay_name()+"");//医生昵称
			if (item.getItem().getExtra_data()!=null) {
				 String strAge = DateUtil.getDayTip(item.getItem().getExtra_data().getBaby_days());
				 holder2.item_age.setText(strAge);//宝宝年龄
			}
			holder2.item_time.setText(DateUtil.gettopicDate(item.getItem().getPost_time()));//发布时间
			if (item.getItem().getAnswer().getPost_actor().getProfile()!=null) {
				holder2.doctor_position.setText(item.getItem().getAnswer().getPost_actor().getProfile().getTitle()+"");//医生所属职称
				holder2.item_hospital.setText(item.getItem().getAnswer().getPost_actor().getProfile().getInstruction());//医生所属医院
			}
		}
		
		
		return convertView;
	}
	public void setView1(ViewHolder1 holder, View view) {
		holder.item_content = (TextView) view
				.findViewById(R.id.item_question_content);
		holder.item_age = (TextView) view.findViewById(R.id.item_age);
		holder.item_time = (TextView) view.findViewById(R.id.item_time);
		holder.item_time = (TextView) view
				.findViewById(R.id.item_time);
	}

	public void setView2(ViewHolder2 holder, View view) {
		holder.item_question_content = (TextView) view
				.findViewById(R.id.item_question_content);
		holder.item_answer_content = (TextView) view
				.findViewById(R.id.item_answer_content);
		holder.head = (AsyncImageView) view.findViewById(R.id.head);
		holder.head.setOnClickListener(this);
		holder.name = (TextView) view.findViewById(R.id.name);
		holder.item_age = (TextView) view.findViewById(R.id.item_age);
		holder.item_hospital = (TextView) view.findViewById(R.id.item_hospital);
		holder.item_hospital.setVisibility(View.VISIBLE);
		holder.item_time = (TextView) view.findViewById(R.id.item_time);
		holder.doctor_position = (TextView) view.findViewById(R.id.doctor_position);
	}
	
	
	class ViewHolder1 {
		TextView item_content;
		TextView item_age;
		TextView item_time;
	}

	class ViewHolder2 {
		TextView item_question_content;
		TextView item_answer_content;
		AsyncImageView head;
		TextView name;
		TextView item_age;
		TextView item_hospital;
		TextView item_time;
		TextView doctor_position;
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

}
